﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace BCSStockInfo.BCSStockInfo
{    
    public class StockInfoService
    {
        static string dynamicSymbologyPath = "root/result/DynamicSymbology/{0}";
        static string dynamicPath = "root/result/Dynamic/{0}";

        /// <summary>
        /// ReadItem
        /// </summary>
        /// <param name="symbol">Stock Symbol</param>
        /// <returns>StockInfoEntity</returns>
        public static StockInfoEntity ReadItem(string symbol)
        {
            try
            {
                string stockUrl = string.Format(Resources.StockUrl, symbol);

                //Create the XmlDocument.
                XmlDocument doc = new XmlDocument();

                //Load the the document.
                doc.Load(stockUrl);

                if (doc != null)
                {
                    //Retrieve the stock information
                    StockInfoEntity info = new StockInfoEntity();

                    info.Symbol = RetriveValue(doc, "Symbol");
                    info.CompanyName = RetriveValue(doc, "CompanyName");
                    info.Country = RetriveValue(doc, "Country");
                    info.Type = RetriveValue(doc, "Type");

                    info.LastQuote = RetriveDecimalValue(doc, "Last");
                    info.Change = RetriveDecimalValue(doc, "Change");
                    info.Close = RetriveDecimalValue(doc, "Close");
                    info.Open = RetriveDecimalValue(doc, "Open");
                    info.High = RetriveDecimalValue(doc, "High");
                    info.Low = RetriveDecimalValue(doc, "Low");
                    info.Volume = RetriveDecimalValue(doc, "Volume");
                    info.Bid = RetriveDecimalValue(doc, "Bid");
                    info.BidSize = RetriveDecimalValue(doc, "BidSize");
                    info.Ask = RetriveDecimalValue(doc, "Ask");
                    info.AskSize = RetriveDecimalValue(doc, "AskSize");
                    info.High52Week = RetriveDecimalValue(doc, "High52Week");
                    info.Low52Week = RetriveDecimalValue(doc, "Low52Week");
                    info.PercentChange = RetriveDecimalValue(doc, "PercentChange");

                    return info;
                }
            }
            catch (Exception)
            {
                //supress the exception
            }
            return new StockInfoEntity();
        }        
        
        /// <summary>
        /// Read List
        /// </summary>
        /// <returns>IEnumerable of StockInfoEntity</returns>
        public static IEnumerable<StockInfoEntity> ReadList()
        {
            List<StockInfoEntity> stockInfo = new List<StockInfoEntity>();

            foreach(string StockSymbol in Resources.StockSymbols)
            {
                stockInfo.Add(ReadItem(StockSymbol.Trim()));
            }

            return stockInfo;
        }

        /// <summary>
        /// Retrieve values
        /// </summary>
        /// <param name="doc">XML document</param>
        /// <param name="parameter">string parameter</param>
        /// <returns>value of the paramter</returns>
        private static string RetriveValue(XmlDocument doc, string parameter)
        {
            string value = doc.SelectSingleNode(String.Format(dynamicSymbologyPath, parameter)).InnerText.Trim();

            if (value == null)
            {
                return String.Empty;
            }

            if (String.IsNullOrWhiteSpace(value))
            {
                return String.Empty;
            }

            return value;
        }

        /// <summary>
        /// Retreive decimal values
        /// </summary>
        /// <param name="doc">XML Document</param>
        /// <param name="parameter">string parameter</param>
        /// <returns>Decimal valu</returns>
        private static decimal RetriveDecimalValue(XmlDocument doc, string parameter)
        {
            string value = doc.SelectSingleNode(String.Format(dynamicPath, parameter)).InnerText;

            if (value == null)
            {
                return 0;
            }

            if (String.IsNullOrWhiteSpace(value))
            {
                return 0;
            }

            decimal result = 0;

            if (Decimal.TryParse(value, out result))
            {
                return result;
            }

            return 0;
        }
    }
}


